home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / external / sharelib / double_array.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-08  |  3.8 KB  |  151 lines

  1. /*
  2. **    $Id: double_array.c,v 1.1 1993/11/16 23:36:16 idl Exp $
  3. **
  4. ** NAME:
  5. **    double_array    
  6. **
  7. ** PURPOSE:
  8. **    This C function is used to demonstrate how to pass a IDL array
  9. **    of type double to a C function using the IDL function CALL_EXTERNAL.
  10. **
  11. ** CATEGORY:
  12. **    Dynamic Link
  13. **
  14. ** CALLING SEQUENCE:
  15. **      This function is called in IDL by using the following command
  16. **
  17. **      IDL> result = CALL_EXTERNAL('double_array.so', '_double_array',    $
  18. **      IDL>            array_var, array_size)
  19. **
  20. ** INPUTS:
  21. **    array_var:    A IDL array of type double 
  22. **
  23. **    array_size:    A IDL scalar long varaible that contains the number
  24. **            of elements in array_var. This is the number that
  25. **            is returned from the IDL function N_ELEMENTS.
  26. **
  27. ** OUTPUTS:
  28. **    This function will return the maximum value of the passed in
  29. **    array. This value is an IDL double scalar value.
  30. **
  31. ** SIDE EFFECTS:
  32. **    The values contained in the array and the number of elements 
  33. **    are written to stdout. 
  34. **
  35. ** RESTRICTIONS:
  36. **    This example assumes that the length value is long (4 bytes) and not
  37. **    a short integer. An IDL integer is only 2 bytes long, so the variables
  38. **    should be delcared in IDL at type long.
  39. **
  40. ** EXAMPLE:
  41. **----------------------------------------------------------------------------
  42. ;; The following are the commands that would be used to call this
  43. ;; routine in IDL.
  44. ;;
  45.            array_var = dindgen(10)*!dpi ;create a double array with values in it.
  46.       array_size = long(n_elements(array_var))    
  47.  
  48.     max = CALL_EXTERNAL('double_array.so', '_double_array',        $
  49.             array_var, array_size)
  50.  
  51. **----------------------------------------------------------------------------
  52. **
  53. ** MODIFICATION HISTORY:
  54. **    Written October, 1993        KDB
  55. **    
  56. ** Declare header file.
  57. */
  58.  
  59. #include <stdio.h>
  60.  
  61. /*
  62. ** Declare a macro for determining the maximum of two values.
  63. ** This is taken from the IDL header file $IDL_DIR/source/export.h
  64. */
  65.  
  66. #ifndef MAX
  67. #define MAX(x,y) (((x) > (y)) ? (x) : (y))
  68. #endif
  69.  
  70. /*
  71. ** Declare the function
  72. */
  73.  
  74. double
  75. double_array(argc, argv)
  76. int argc;
  77. void *argv[];
  78. {
  79. /*
  80. ** Declare local variables
  81. */
  82.    double    *double_array;    /* pointer to the double array     */
  83.    short     n_elements;    /* number of elements in array    */
  84.    double     max_value;     /* used to hold max value    */
  85.    short      i;        /* Counter            */
  86.  
  87. /*
  88. ** Insure that the correct number of arguments were passed in (argc = 2).
  89. */
  90.    if(argc != 2)
  91.    {
  92.    /*
  93.    ** Print an error message and return.
  94.    */
  95.       fprintf(stderr, "double_array: Incorrect number of arguments\r\n");
  96.       return(-1.0);
  97.    }
  98. /*
  99. ** Cast the pointers in argv to local variables.
  100. */
  101.    double_array    = (double *) argv[0];
  102.    n_elements    = (short)(*(long *)argv[1]); /* deref pointer, cast 2 short */
  103.  
  104. /*
  105. ** Check the size of the array passed in. n_elements should be > 0.
  106. */
  107.    if( n_elements < 1)
  108.    {
  109.    /*
  110.    ** Print an error message and return
  111.    */
  112.       fprintf(stderr, "double_array: Array elements is less that one\r\n");
  113.       return(-1.0);
  114.    }
  115. /*
  116. ** Set max to the first value in the array
  117. */
  118.    max_value = *double_array;
  119.  
  120. /*
  121. ** Print out the passed in information
  122. */
  123.    fprintf(stdout,
  124.     "\r\n-----------------------------------------------------\r\n");
  125.    fprintf(stdout,"Inside C function double_array ");
  126.    fprintf(stdout,"(Called from IDL using CALL_EXTERNAL)\r\n\r\n");
  127.  
  128. /*
  129. ** Now print the values of each variable that was passed in from IDL
  130. */
  131.    fprintf(stdout,"Number of elements in the Double array: %d\r\n",n_elements); 
  132.  
  133.    fprintf(stdout, "Double Array Contents:\r\n");
  134.    for(i=0; i < n_elements; i++)
  135.     fprintf(stdout,"\tElement %3d:   %f\r\n",i, double_array[i]);
  136.  
  137.    fprintf(stdout,"\n-----------------------------------------------------\n");
  138.  
  139. /*
  140. ** Now lets find the max value in the array. Just use a for loop.
  141. */
  142.    for(i=1; i < n_elements; i++)
  143.       max_value = MAX(max_value, double_array[i] );   
  144.  
  145. /*
  146. ** Now return the max value of the array to IDL (Call_External).
  147. */
  148.    return(max_value);
  149. }
  150.  
  151.